// 导入数据库操作模块
const db = require('../db/index')

// 获取菜单基本信息的处理函数
exports.getMenuinfo = (req, res) => {
    

    let params = req.query || req.params

    console.log(params,'111')

    let sql = "SELECT * FROM menu WHERE is_delete = 0"

    let content = []
    // let isMore = false //是否有多个查询参数

    if (params.username) {
        sql += ` AND menuname LIKE ? `
        content.push(params.menuname)
        isMore = true;
    }
    if (params.status) {
        sql += " AND status LIKE ? "; //and是两个条件都必须满足，or是或的关系
        content.push(params.status);
    }

    if (params.pageNun || params.pageSize) { //开启分页
        let current = params.pageNun; //当前页码
        let pageSize = params.pageSize; //一页展示多少条数据
        sql += " limit ?,?";
        content.push((current - 1) * pageSize, parseInt(pageSize));
    }
    console.log(sql)
    // 执行sql
    db.query(sql, content, (err, results) => {

        // 执行sql语句失败
        if (err) return res.cc(err)
        // console.log(results)
        // 通过递归 产生N级菜单
        function constructTree(children, id) {
            results.map(item => {
                if (item.parent_id === id) {
                    let treeItem = {
                        id: item.id,
                        pid: item.parent_id,
                        name: item.menu_name,
                        path: item.menu_url,
                        children: [],
                        icon:item.icon,
                        status:item.status,
                        createTime:new Date(item.create_time).getFullYear() + "-" + (new Date(item.create_time).getMonth() + 1 < 10 ? '0' + (new Date(item.create_time).getMonth() + 1) : (new Date(item.create_time).getMonth() + 1)) + "-" + new Date(item.create_time).getDate() + " " + new Date(item.create_time).getHours() + ":" + (new Date(item.create_time).getMinutes() < 10 ? '0' + new Date(item.create_time).getMinutes() : new Date(item.create_time).getMinutes())
                    }
                    constructTree(treeItem.children, treeItem.id)
                    children.push(treeItem)
                }
            })
        }
            let treeData = []
            results.forEach(element => {
                if (element.parent_id === 0) {
                    let treeItem = {
                        id: element.id,
                        pid: element.parent_id,
                        name: element.menu_name,
                        path: element.menu_url,
                        children: [],
                        icon:element.icon,
                        status:element.status,
                        createTime:new Date(element.create_time).getFullYear() + "-" + (new Date(element.create_time).getMonth() + 1 < 10 ? '0' + (new Date(element.create_time).getMonth() + 1) : (new Date(element.create_time).getMonth() + 1)) + "-" + new Date(element.create_time).getDate() + " " + new Date(element.create_time).getHours() + ":" + (new Date(element.create_time).getMinutes() < 10 ? '0' + new Date(element.create_time).getMinutes() : new Date(element.create_time).getMinutes())
                    }
                    constructTree(treeItem.children, treeItem.id)
                    treeData.push(treeItem)
                }

            });

        // 执行sql语句成功，但查询结果为空
        // if (results.length !== 1) return res.cc('获取菜单失败')

        // 获取用户信息成功
        res.send({
            status: 0,
            message: "获取菜单成功",
            data: treeData,
            total:results.length
        })

    })

}

